﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LCHWinScraper.SiteScrapers;
using System.Reflection;

namespace LCHWinScraper
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();

            Text = string.Format(Text, Assembly.GetEntryAssembly().GetName().Version.ToString(2));
			LoadSitesList();
		}

		private void LoadSitesList()
		{
			var scrapers = new List<SiteScraper>();
			scrapers.Add(new AS());
			scrapers.Add(new LPS());
			scrapers.Add(new RECON());
			scrapers.Add(new NdsCorp());
			scrapers.Add(new PRIOR());
			scrapers.Add(new ASLVREO());

			cbSites.DataSource = scrapers;
			cbSites.DisplayMember = "Name";
			cbSites.ValueMember = "Value";
		}

		private void btnParse_Click(object sender, EventArgs e)
		{
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
			try
			{
				ClearErrorMsg();
				((SiteScraper)cbSites.SelectedItem).ParseDataProcess();
                MessageBox.Show("Page Scraped and Uploaded Successfully");
			}            
			catch (Exception ex)
			{
				MessageBox.Show("Some error has occured or no data for scrapping found.\n" + ex.Message + "\n" + ex.StackTrace);
			}
            finally
            {
                System.Windows.Forms.Cursor.Current = Cursors.Default;
            }
		}

		private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			var scraper = (SiteScraper) cbSites.SelectedItem;
            
			if (scraper is AS)
			{
				
			}
			else if (scraper is LPS && webBrowser1.Url.AbsoluteUri.Equals(scraper.DataUrl))
			{
                var target = webBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_gridTS");
				if (target != null)
					scraper.HtmlContent = target.InnerHtml;
			}
			else if (scraper is RECON /*&& webBrowser1.Url.AbsoluteUri.Equals(scraper.DataUrl)*/)
			{
				//scraper.HtmlContent = webBrowser1.DocumentText;
			}
			else if (scraper is NdsCorp && webBrowser1.Url.AbsoluteUri.Equals(scraper.DataUrl))
			{
				var target = webBrowser1.Document.GetElementById("upListView");
				if (target != null)
					scraper.HtmlContent = target.InnerHtml;
			}
			else if (scraper is PRIOR && webBrowser1.Url.AbsoluteUri.Equals(scraper.DataUrl))
			{
				var target = webBrowser1.Document.GetElementById(/*"ctl00_ContentPlaceHolder1_DataPager1"*/"itemPlaceholderContainer");
				if (target != null)
					scraper.HtmlContent = webBrowser1.DocumentText.Replace("\r\n", "").Replace("\t", "");//target.InnerHtml; //webBrowser1.DocumentText.Replace("\r\n", "").Replace("\t", "");
			}
			else if (scraper is ASLVREO)
			{
			}
			else
				return;

			btnParse.Enabled = true;
		}

		private void btnNavigate_Click(object sender, EventArgs e)
		{
			ClearErrorMsg();
			webBrowser1.Navigate(((SiteScraper)cbSites.SelectedItem).StartUrl);
			btnParse.Enabled = false;
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			ClearErrorMsg();
		}

		private void ClearErrorMsg()
		{
			
		}

		private void cbSites_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (cbSites.SelectedItem is AS || cbSites.SelectedItem is ASLVREO)
			{
				btnNavigate.Enabled = false;
				btnParse.Enabled = true;
			}
			else
			{
				btnNavigate.Enabled = true;
				btnParse.Enabled = false;
			}

            //btnParse.Enabled = true;
		}
	}
}
